# -*- coding: utf-8 -*-

from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.appium_service import AppiumService
from appium.webdriver.common.appiumby import AppiumBy
import time
import pytest

'''
# 一个基于AVD的Appium自动化测试demo
#   内容:打开AVD的联系人页面,新建一个联系人HelloAppium,联系电话12345678
# !前提: Appium服务器开启、AVD模拟器开启
'''

APPIUM_HOST = '127.0.0.1'
APPIUM_PORT = 4723

# !启动Appium服务端
@pytest.fixture(scope='session')
def appium_server():
    server = AppiumService()
    server.start(args=['--address', APPIUM_HOST, '-p', str(APPIUM_PORT)], timeout_ms=60000)
    yield server
    server.stop()

# 建立客户端与服务端的连接会话
def create_android_session(custom_opts = None):
    options = UiAutomator2Options()
    if custom_opts is not None:
        options.load_capabilities(custom_opts)
    return webdriver.Remote(f'http://{APPIUM_HOST}:{APPIUM_PORT}', options=options)


def test_avd_add_contact(appium_server):
    opts = {
        "appPackage": "com.google.android.dialer",
        "appActivity": ".extensions.GoogleDialtactsActivity",
    }

    driver = create_android_session(custom_opts=opts)
    
    # 打开拨号程序
    contactBotton = driver.find_element(AppiumBy.ID, value="com.google.android.dialer:id/tab_contacts")
    contactBotton.click()

    #create_new_contact = driver.find_element(by='-android uiautomator', value='text("Create new contact")')
    create_new_contact = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("Create new contact")')
    create_new_contact.click()
    # !此处不延时会因为页面还未跳转，后续查找元素执行失败
    time.sleep(1)

    #name = driver.find_element(by='-android uiautomator', value='text("First name")')
    name = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("First name")')
    name.click()
    name.send_keys("HelloAppium")

    #time.sleep(1)
    # 模拟点击其他地方跳出电话编辑框， 方便后面的滑动操作
    ret_from_edit = driver.find_element(AppiumBy.ID, value='com.google.android.contacts:id/expansion_view')
    ret_from_edit.click()

    #time.sleep(1)
    # 需要滑动一下，否则输入电话号码的界面会被键盘遮挡住
    #size = driver.get_window_size()
    #x = size["width"] * 0.5
    #y = size["height"] * 0.8
    #y1 = size["height"] * 0.4
    #driver.swipe(start_x=x, start_y=y, end_x=x, end_y=y1)
    #time.sleep(1)

    #phone = driver.find_element(by='-android uiautomator', value='text("Phone")')
    # 模拟输入电话号码
    phone = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Phone").instance(0))')
    phone.click()
    phone.send_keys("12345678")

    # 点击保存按钮保存
    save = driver.find_element(AppiumBy.ID, value="com.google.android.contacts:id/toolbar_button")
    save.click()

    # 进入保存的联系人页面
    contact_save = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("HelloAppium")')
    contact_save.click()
    time.sleep(1)

    # 保存页面截图
    driver.save_screenshot("add_one_contact.png")

    # 校验电话号码是否n正确
    phone_number_save = driver.find_element(AppiumBy.ID, value="com.google.android.contacts:id/header")
    assert phone_number_save.text == "1 (234) 567-8"